package org.zt.ccty.springboot_mybatis_demo.zk;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * Description:
 *
 * @author zhoutao
 * @since 2017/11/27
 */
@Service
public class ZkConfig {

	@Value("${zookeeper_230_address}")
    private String ZOOKEEPER_230_ADDRESS;
	
	@Value("${zookeeper_210_address}")
    private String ZOOKEEPER_210_ADDRESS;
	
	@Value("${env_230}")
    private String ENV_230;
	
	@Value("${env_210}")
    private String ENV_210;

	public String getZKAddress(String env) {
        if (ENV_230.equalsIgnoreCase(env)) {
            return ZOOKEEPER_230_ADDRESS;
        } else if (ENV_210.equalsIgnoreCase(env)) {
            return ZOOKEEPER_210_ADDRESS;
        } else {
            return null;
        }
    }

    public String getDubboZkUrl(String env) {
        String zkAddress = getZKAddress(env);
        if (zkAddress == null) {
            return null;
        }
        String[] ips = zkAddress.split(",");
        StringBuffer dubboZkUrl = new StringBuffer("zookeeper://");
        dubboZkUrl.append(ips[0]).append("?backup=");
        dubboZkUrl.append(ips[1]).append(",");
        dubboZkUrl.append(ips[2]);
        return dubboZkUrl.toString();
    }
}
